package at.fhooe.pandora.entities;

import at.fhooe.engine.entity.Entity;
import at.fhooe.engine.event.events.CollisionEvent;
import at.fhooe.engine.pandora.Game;
import at.fhooe.engine.physics.CircleBody;
import at.fhooe.engine.physics.PhysicBody;

public class ResourceEntity extends Entity implements CollisionEvent.Listener {

	public static final String ID = "ResourceEntity";
	
	public static final int TYPE_STONE = 0;
	public static final int TYPE_GOLD = 1;
	public static final int TYPE_COPPER = 2;
	
	public static final int SIZE = 12;
	
	private int m_type;
	private PhysicBody m_physicBody;
	
	public ResourceEntity(String name, String timerName, double positionX, double positionY, double positionZ, double velX, double velY, double velZ, int type) {
		super(ID, name);
		
		m_type = type;
		
		m_physicBody = new CircleBody(name, SIZE, true);
		m_physicBody.setTimer(timerName);
		m_physicBody.setPosition(positionX, positionY, positionZ);
		m_physicBody.setVelocity(velX, velY, velZ);
	}
	
	public double getPositionX() {
		return m_physicBody.getPositionX();
	}
	
	public double getPositionY() {
		return m_physicBody.getPositionY();
	}
	
	public void setPosition(double posX, double posY, double posZ) {
		m_physicBody.setPosition(posX, posY, posZ);
	}

	public PhysicBody getPhysicBody(){
		return m_physicBody;
	}
	
	
	
	@Override
	protected void initialize() {
		Game.getInstance().getPhysicsManager().addPhysicBody(m_physicBody);
		Game.getInstance().getEventManager().addListener(this, CollisionEvent.TYPE);
	}

	@Override
	protected void deinitialize() {
		Game.getInstance().getPhysicsManager().removePhysicBody(m_physicBody);
		Game.getInstance().getEventManager().removeListener(this, CollisionEvent.TYPE);
	}

	@Override
	public void update() {
		limitWorldPosition();
	}
	
	//TODO remove duplicated code
	private void limitWorldPosition() {
		int offset = SIZE;
		//TODO The state should handle this
		int width = (int) (	Game.getInstance().getGraphicsSystem().getWidth() /
				Game.getInstance().getGraphicsSystem().getScaleX());
		int height = (int) (Game.getInstance().getGraphicsSystem().getHeight() /
				Game.getInstance().getGraphicsSystem().getScaleY());

		if (m_physicBody.getPositionX() > width + offset) {
			m_physicBody.setPosition(0 - offset, m_physicBody.getPositionY(), 0.0);
		} else if (m_physicBody.getPositionX() < 0 - offset) {
			m_physicBody.setPosition(width + offset, m_physicBody.getPositionY(), 0.0);
		}
		
		if (m_physicBody.getPositionY() > height + offset) {
			m_physicBody.setPosition(m_physicBody.getPositionX(), 0 - offset, 0.0);
		} else if (m_physicBody.getPositionY() < 0 - offset) {
			m_physicBody.setPosition(m_physicBody.getPositionX(), height + offset, 0.0);
		}
	}
	
	public int getType() {
		return m_type;
	}

	@Override
	public void onCollisionEvent(CollisionEvent collisionEvent) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public double[] getDebug() {
		// TODO Auto-generated method stub
		return null;
	}

}
